﻿using System.Windows;
using System.Windows.Input;
using SnakeGame.ViewModels;
using SnakeGame.Models;

namespace SnakeGame.Views
{
    /// <summary>
    /// Interaction logic for StepWindow.xaml
    /// </summary>
    public partial class StepWindow : Window
    {
        public StepWindowViewModel _stepWindowViewModel;
        public int GoValue { get; set; }
        public bool IsCancelled { get; set; }
        public StepWindow()
        {
            InitializeComponent();
        }

        public void Initialize(Player p)
        {
            _stepWindowViewModel = new StepWindowViewModel();
            _stepWindowViewModel.RequestClose += _stepWindowViewModel_RequestClose;
            _stepWindowViewModel.Initialize(p);
            DataContext = _stepWindowViewModel;

            FocusManager.SetFocusedElement(this, GoTextBox);
        }

        void _stepWindowViewModel_RequestClose(object sender, System.EventArgs e)
        {
            IsCancelled = _stepWindowViewModel.IsCancel;
            GoValue = (_stepWindowViewModel.GoValue.HasValue == true && _stepWindowViewModel.IsCancel == false) ? _stepWindowViewModel.GoValue.Value : 0;
            Close();
        }

        private void GoTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                _stepWindowViewModel.OkCommand.Execute(sender);
            }
            else if (e.Key == Key.Escape)
            {
                _stepWindowViewModel.CancelCommand.Execute(sender);
            }
        }
    }
}
